package pow;

/**
 * @author hht
 * @ClassName NoRecusion
 * @Description  非递归的实现方式
 * @Date 2019/6/5 15:18
 * @VERSION 1.0
 */
public class NoRecusion {
    public static void main(String[] args) {
        long begin = System.currentTimeMillis();
        System.out.println(myPow(1,-2147483648));
        System.out.println(System.currentTimeMillis()-begin);
    }
    public static double myPow(double x, int n) {
        if(n<0)
            x=1/x;

        double total=1;
        double workingTotal=x;

        long N=0;
        long workingN=1;
        long absN=Math.abs((long)n);

        while(N<absN) {
            while(N+(workingN*2)<absN) {
                workingN*=2;
                workingTotal*=workingTotal;
            }

            N+=workingN;
            total*=workingTotal;

            workingN=1;
            workingTotal=x;
        }

        return total;
    }
}
